package cn.nfwx.tv.fragment.bus;

import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.nfwx.tv.R;
import cn.nfwx.tv.controller.BusAnimController;
import cn.nfwx.tv.base.BaseDialogFragment;
import cn.nfwx.tv.util.ToolUtil;
import cn.nfwx.tv.widget.AnimationsContainer;

public class BusSceneFragment extends BaseDialogFragment implements Handler.Callback {
    private ImageView imageView;
    private TextView scene_tips;

    private int[] bus_in;
    private int[] bus_leave;

    private OnAnimationFinishListener onAnimationFinishListener;
    private AnimationsContainer.FramesSequenceAnimation animation;

    private int mStats;

    public static BusSceneFragment newInstance(int stats) {
        BusSceneFragment fragment = new BusSceneFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("stats", stats);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_bus;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        imageView = findView(R.id.imageView);
        scene_tips = findView(R.id.scene_tips);

        bus_in = ToolUtil.getRes(getActivity(), R.array.bus_in);
        bus_leave = ToolUtil.getRes(getActivity(), R.array.bus_leave);

        Bundle bundle = getArguments();
        int stats = bundle.getInt("stats", -1);

        setAnimationDrawable(stats);
    }

    /**
     * 开启动画
     *
     * @param stats
     */
    public void setAnimationDrawable(int stats) {
        if (mStats == stats) {
            return;
        }
        mStats = stats;
        if (animation != null) {
            animation.setmOnAnimationStoppedListener(null);
            animation = null;
        }
        if (stats == BusAnimController.STATS_BUS_IN) {
            scene_tips.setText("正在切换公交环境");
            animation = AnimationsContainer.createAnim(imageView, bus_in, 100);
        } else if (stats == BusAnimController.STATS_BUS_LEAVE) {
            scene_tips.setText("您已离开公交，正在为你切换网络环境");
            animation = AnimationsContainer.createAnim(imageView, bus_leave, 100);
        }
        if (animation == null) {
            dismissAllowingStateLoss();
            return;
        }
        animation.setmOnAnimationStoppedListener(new AnimationsContainer.OnAnimationStoppedListener() {
            @Override
            public void onAnimationStart() {
                if (onAnimationFinishListener != null) {
                    onAnimationFinishListener.onStart();
                }
            }

            @Override
            public void onAnimationStopped() {
                if (onAnimationFinishListener != null) {
                    onAnimationFinishListener.onFinish();
                }
            }
        });
        animation.start();
        handler.sendEmptyMessageDelayed(0, 3000);
    }


    Handler handler = new Handler(this);


    /**
     * 是否填满整个屏幕
     */
    protected boolean isMatchParent() {
        return true;
    }

    protected boolean isOutsideTouchable() {
        return true;
    }

    //是否透明背景
    protected boolean isTranslucentBackground() {
        return true;
    }

    protected <T extends View> T findView(int id) {
        return (T) getView().findViewById(id);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        animation.stop();
        super.onDismiss(dialog);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                animation.stop();
                dismissAllowingStateLoss();
                break;
        }
        return false;
    }

    public void setData(int data) {
        Bundle bundle = new Bundle();
        bundle.putInt("stats", data);
        setArguments(bundle);
    }

    public interface OnAnimationFinishListener {
        void onStart();

        void onFinish();
    }

    public void setOnAnimationFinishListener(OnAnimationFinishListener onAnimationFinishListener) {
        this.onAnimationFinishListener = onAnimationFinishListener;
    }
}
